package com.software.cool.api.app.v1.service.impl;
import cn.hutool.core.util.RandomUtil;
import com.software.cool.api.app.v1.vo.AppVO;
import com.software.cool.api.app.v1.vo.ProjectTestDataVO;
import com.software.cool.entity.TbSysUser;
import com.software.cool.mapper.TbRoleMapper;
import com.software.cool.mapper.TbSysAppMapper;
import com.software.cool.api.admin.v1.vo.AdminLoginUserVO;
import com.software.cool.api.app.v1.service.IAppV1Service;
import com.software.cool.common.CommonContant;
import com.software.cool.entity.MenuInfo;
import com.software.cool.service.impl.TbRoleMenuService;
import com.software.cool.service.impl.TbSysUserService;
import com.software.cool.utils.CommonUtils;
import com.software.cool.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AppV1Service implements IAppV1Service {

    @Resource
    private TbSysUserService sysUserService;
    @Resource
    private TbSysAppMapper sysAppMapper;
    @Resource
    private TbRoleMenuService roleMenuService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private TbRoleMapper roleMapper;
    @Value("${swagger.host}")
    private String basePath;
    @Value("${file.oss}")
    private boolean fileOss;
    @Value("${server.port}")
    private Integer serverPort;


    @Override
    public AdminLoginUserVO login(String userName, String password, HttpServletRequest request) {
        AdminLoginUserVO loginUser=null;
        try{
            String path = request.getContextPath();
            String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
            if(CommonUtils.isNotBlank(userName) && CommonUtils.isNotBlank(password)){
//                password=SecureUtil.md5(password);
                TbSysUser model=new TbSysUser();
                model.setUserName(userName);
                model.setPassword(password);
                TbSysUser sysUser=sysUserService.getLogin(userName,password);
                if(sysUser!=null){
                    loginUser=new AdminLoginUserVO();
                    List<MenuInfo> menuInfo=roleMenuService.getMenuInfo(sysUser.getRoleId());
                    BeanUtils.copyProperties(sysUser,loginUser);
                    loginUser.setMenuInfo(menuInfo);
                    String token= RandomUtil.randomString(16);
                    loginUser.setToken(token);
                    Date tokenDate= DateUtils.minOffset(new Date(),59);
                    loginUser.setTokenTime(tokenDate.getTime());
                    loginUser.setFileOss(fileOss);
                    loginUser.setBasePath(basePath);
                    HttpSession session = request.getSession();
                    session.setAttribute(CommonContant.APP_LOGIN_SESSION, loginUser);
                }
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return loginUser;
    }

    @Override
    public Long getTokenTime(String token) {
        Long result=0L;
        HttpSession session = request.getSession();
        AdminLoginUserVO loginUser = (AdminLoginUserVO) session.getAttribute(CommonContant.APP_LOGIN_SESSION);
        if(loginUser!=null && loginUser.getToken().equals(token))
        {
            result=1L;
        }
        return result;
    }


    @Override
    public AppVO getLastedApp(HttpServletRequest request) {
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
        AppVO app=sysAppMapper.getLastedApp();
        if(app!=null){
            String newAppLogo=basePath+"/admin/v1/common/showImageByServer?filePath="+app.getNewAppLogo();
			app.setNewAppLogo(newAppLogo);
            String newAppUrl=basePath+"/admin/v1/common/downloadApk?filePath="+app.getNewAppUrl();
            app.setNewAppUrl(newAppUrl);
        }
        return app;
    }


}
